package com.kaizige.realtor.Dao;

import com.kaizige.realtor.Pojo.tenant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class tenantDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	// 查询所有的租客
	public List<Map<String,Object>> queryAllTen(){
		String sql = "select * from tenant";
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		return maps;
	}
	
	// 增加租客信息
	public void addTen(tenant t){
		String sql = "insert into tenant values (?,?,?,?,?,?,?);";
		Object[] args = {t.getName(), t.getAddress(), t.getPhone(),
				t.getBirth(), t.getGender(),t.getPassword(),0.0};
		jdbcTemplate.update(sql,args);
	}
	
	// 按名字查找租客信息
	public Map<String, Object>queryByName(String userName){
		Map<String, Object> map;
		try{
			String sql = "select * from tenant where name=?";
			map = jdbcTemplate.queryForMap(sql, userName);
		}catch(EmptyResultDataAccessException e){
			return null;
		}
		return map;
	}
	
	// 扣钱
	public void paySeeHou(String name){
		String sql = "update tenant set balance = balance - 50 where name=?";
		jdbcTemplate.update(sql,name);
	}
}
